package com.young.aidl_base_client;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.young.aidl_base_sdk.ICalculator;
import com.young.aidl_base_sdk.ICalculatorListener;
import com.young.aidl_base_sdk.bean.Sample;
import com.young.aidl_base_sdk.bean.Sample2;

import java.io.File;

public class MainActivity1 extends AppCompatActivity {

    public static final String TAG = "Young-AIDL-Client-" + MainActivity1.class.getSimpleName();

    // AIDL 接口
    private ICalculator mCalculator;

    // 服务的连接回调
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 将 IBinder 转为具体的 AIDL 接口
            mCalculator = ICalculator.Stub.asInterface(service);
            Log.d(TAG, "onServiceConnected: mCalculator="+mCalculator.hashCode()+" service = "+service);
            callListener();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mCalculator = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initClick();
        bindToServer();
    }

    private void bindToServer() {
        Intent intent = new Intent();
        intent.setAction("com.young.CUSTOMER_SERVICE");
        intent.setComponent(new ComponentName("com.young.aidl_base_server",
                "com.young.aidl_base_server.CalculatorService"));
        boolean connected = bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
        Log.e(TAG, "bindToServer: " + connected);
    }

    private void initClick() {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_do1:
//                        optionParcel();
//                        optionOut();
                        optionBundle();
                        break;
                    case R.id.btn_do2:
                        transactFileDescriptor();
                        break;
                    case R.id.btn_do3:
                        optionTimeOut();
                        break;
                    case R.id.btn_do4:
                        optionOneWay();
                        break;
                    case R.id.btn_do5:
                        callListener();
                        break;
                }

            }
        };

        findViewById(R.id.btn_do1).setOnClickListener(listener);
        findViewById(R.id.btn_do2).setOnClickListener(listener);
        findViewById(R.id.btn_do3).setOnClickListener(listener);
        findViewById(R.id.btn_do4).setOnClickListener(listener);
        findViewById(R.id.btn_do5).setOnClickListener(listener);
    }

    private void optionOneWay() {
        if (mCalculator == null) {
            bindToServer();
            return;
        }
        try {
            mCalculator.optionOneWay();
            Log.e(TAG, "optionOneWay 耗时方法调用完成了");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void optionTimeOut() {
        if (mCalculator == null) {
            bindToServer();
            return;
        }
        try {
            mCalculator.optionTimeOut();
            Log.e(TAG, "耗时方法调用完成了");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void transactFileDescriptor() {
        if (mCalculator == null) {
            bindToServer();
            return;
        }
        File file = new File(Environment.getExternalStorageDirectory(), "Video_2016-11-11_225450.avi");
        ParcelFileDescriptor fileDescriptor = null;
        try {
            Log.e(TAG, "客户端 开始传输大文件 " + file.getName() + " " + file.length() + " " + System.currentTimeMillis());
            fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
            mCalculator.transactFileDescriptor(fileDescriptor);
        } catch (Exception e) {
            e.printStackTrace();
        }

        long k = 1694838290695L - 1694838285694L;
        Log.e(TAG, "k = " + k);

    }

    private void optionParcel() {
        Sample sample = new Sample();
        sample.setNum(999);
        sample.setSample2(new Sample2(33));
        try {
            ApplicationInfo appInfo = getApplicationInfo();

            String processName = appInfo.processName;
            Log.e(TAG, "hello pid=" + processName);
            mCalculator.optionParcel(sample);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void optionOut() {
        Sample sample = new Sample();
        try {
            Log.e(TAG, this + " optionOut before=" + sample);
            mCalculator.optionOut(sample);
            Log.e(TAG, this + " optionOut after=" + sample);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void optionBundle() {
        Bundle bundle = new Bundle();

        Sample sample = new Sample();
        sample.setNum(999);
        sample.setSample2(new Sample2(33));

        bundle.putParcelable("key1", sample);
        bundle.putSerializable("key2", new Sample2(66));

        try {
            if (mCalculator == null) {
                bindToServer();
            }
            mCalculator.optionBundle(bundle);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private void callListener() {
        ICalculatorListener.Stub listener = new ICalculatorListener.Stub() {
            @Override
            public void onCallback(String result) throws RemoteException {
                Log.e(TAG,"客户端接收到服务器主动返回的数据："+result);
            }
        };
        try {
            mCalculator.registerListener(listener);
            Log.e(TAG,"客户端注册了回调");

//            Thread.sleep(8000);
            Thread.sleep(1000);
//            mCalculator.unRegisterListener(listener);
//            Log.e(TAG,"客户端注销了回调");
        } catch (RemoteException | InterruptedException e) {
            Log.e(TAG,"客户端注册了回调失败 "+e.getMessage());
        }

    }
}